package com.itheima.pinda.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.pinda.common.utils.PageResponse;
import com.itheima.pinda.common.utils.Result;
import com.itheima.pinda.entity.Member;
import com.itheima.pinda.service.IMemberService;
import lombok.extern.log4j.Log4j2;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@Log4j2
@RestController
@RequestMapping("member")
public class MemberController {
    @Autowired
    private IMemberService memberService;

    /**
     * 新增
     *
     * @param entity
     * @return
     */
    @PostMapping("")
    public Result save(@RequestBody Member entity) {
        // 判断
        // 新增
        if(entity!=null){
            Member member = new Member();
            BeanUtils.copyProperties(entity,member);
            member.setId(entity.getId());
            memberService.save(member);
            return Result.ok("新增成功");
        }
        return Result.error();
    }

    /**
     * 详情
     *
     * @param id
     * @return
     */
    @GetMapping("detail/{id}")
    public Member detail(@PathVariable(name = "id") String id) {
        // 判断
        if(StringUtils.isNotBlank(id)){
            // 再查询
            Member member = memberService.getById(id);
            return member;
        }
        return null;
    }

    /**
     * 分页查询
     *
     * @param page
     * @param pageSize
     * @return
     */
    @GetMapping("page")
    public PageResponse<Member> page(Integer page, Integer pageSize) {
        // 定义出page
        IPage<Member> page1 = new Page<>(page,pageSize);
        // 再查询其他条件，拼接分页条件(想不出)
        // 1.利用myplus提供的lomda表达式
        LambdaQueryWrapper<Member> wrapper = new LambdaQueryWrapper<>();
        // 参数里面没有其他参数，直接调用提供的page方法
        IPage<Member> pageResult = memberService.page(page1, wrapper);

        // 组合分页查询
       return PageResponse.<Member>builder().items(pageResult.getRecords()).page(page).pages(pageResult.getPages()).pagesize(pageSize).counts(pageResult.getTotal()).build();
    }

    /**
     * 修改
     *
     * @param id
     * @param entity
     * @return
     */
    @PutMapping("/{id}")
    public Result update(@PathVariable(name = "id") String id, @RequestBody Member entity) {
        // 判断
        if(StringUtils.isNotBlank(id)){
            // 先查询
            entity.setId(id);
            // 更新
            memberService.updateById(entity);
            return Result.ok();
        }
        return Result.error();
    }

    /**
     * 删除
     *
     * @param id
     * @return
     */
    @DeleteMapping("/{id}")
    public Result del(@PathVariable(name = "id") String id) {
        // 判断
        if(StringUtils.isNotBlank(id)){
            // 根据id删除
            boolean meber = memberService.removeById(id);
            if(meber) {
                return Result.ok();
            }
        }
        return Result.error();
    }
}
